package pe.com.bbva.mantenimiento.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pe.com.bbva.mantenimiento.dao.TablaDAO;
import pe.com.bbva.mantenimiento.domain.Tabla;
import pe.com.bbva.util.Constantes;
import pe.com.stefanini.core.dao.GenericDAOImpl;
import pe.com.stefanini.core.exceptions.BOException;
import pe.com.stefanini.core.exceptions.DAOException;

@Service("tablaDAO")
public class TablaDAOImpl extends GenericDAOImpl<Tabla> implements TablaDAO {

	@Autowired
	public TablaDAOImpl(SessionFactory sessionFactory) {
		super(sessionFactory);
	}
	
	@SuppressWarnings("unchecked")
	public List<Tabla> findDetalleTabla(Long idTabla) throws BOException, DAOException {
		List<Tabla> listaTablas = null;
		List<Long> parametros = new ArrayList<Long>();
		parametros.add(idTabla);
		listaTablas = super.executeListNamedQuery("listaDetalleTabla", parametros);
		return listaTablas;
	}
	
	@SuppressWarnings("unchecked")
	public List<Tabla> findTablas(Tabla tabla) throws BOException, DAOException {
		String where = "";
		if(tabla.getDescripcion()!=null && !tabla.getDescripcion().equals("")){
			where = where + (where==""?"":" AND ") + "upper(descripcion) like upper('%" + tabla.getDescripcion()+ "%')";
		}
		if(tabla.getCodigo()!=null && !tabla.getCodigo().equals("")){
			where = where + (where==""?"":" AND ") + "upper(codigo) like upper('%" + tabla.getCodigo()+ "%')";
		}
		if(tabla.getAbreviado()!=null && !tabla.getAbreviado().equals("")){
			where = where + (where==""?"":" AND ") + "upper(abreviado) like upper('%" + tabla.getAbreviado()+ "%')";
		}
		if(tabla.getEstado()!=null && !tabla.getEstado().equals("")){
			where = where + (where==""?"":" AND ") + "estado like '%" + tabla.getEstado()+ "%'";
		}
		if(tabla.getValor()!=null && !tabla.getValor().equals("")){
			where = where + (where==""?"":" AND ") + "valor = '" + tabla.getValor()+ "'";
		}
		if(tabla.getLabel()!=null && !tabla.getLabel().equals("")){
			where = where + (where==""?"":" AND ") + "label = '" + tabla.getLabel()+ "'";
		}
		if(tabla.getPadre()!= null && tabla.getPadre().getId()!=null){
			where = where + (where==""?"":" AND ") + "padre.id = '"+tabla.getPadre().getId()+"'";
		}
		if(tabla.getId()!=null && !tabla.getId().equals("")){
			where = where + (where==""?"":" AND ") + "id = '" + tabla.getId()+ "'";
		}
		if(!where.equals("")){
			where = Constantes.WHERE_CLAUSE + where;
		}
		String orders = " order by fechaCreacion desc,padre.id,codigo, descripcion ";
		List<Tabla> listaTablas = super.executeQuery(Tabla.class,where,orders);
		return listaTablas;
	}
	
	@SuppressWarnings("unchecked")
	public Tabla findDescripcionTabla(Long id, Long idPadre) throws BOException, DAOException {
		Tabla tabla = null;
		String where =  " where upper(id_tabla) = "+id;
		if(idPadre!= null){
					where = where +" and upper(id_tabla_padre) = "+idPadre;
				}
		String orders = "";
		
		List<Tabla> listaTablas = super.executeQuery(Tabla.class, where,orders);
		if (listaTablas != null && !listaTablas.isEmpty())
			tabla = listaTablas.get(0);
		return tabla;
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public List<Tabla> findTablasPadre(Tabla tabla)throws BOException, DAOException{
		String where = "select o from "+Tabla.class.getName()+" o where o.padre.id is null and o.estado like '%" + (tabla.getEstado() != null && !tabla.getEstado().equals("")? tabla.getEstado().trim() :""  )
		+ "%'";
		if (tabla.getAbreviado() != null && !tabla.getAbreviado().trim().equals("")) {
			where = where + " and upper(o.abreviado) like upper('%"
					+ tabla.getAbreviado().trim() + "%') ";
		}
		if (tabla.getDescripcion() != null && !tabla.getDescripcion().trim().equals("")) {
			where = where + " and upper(o.descripcion) like upper('%" + tabla.getDescripcion().trim()
					+ "%')";
		}
		where=where+" order by fechaCreacion desc,codigo, descripcion";
		List<Tabla> lista = super.executeHqlParam(where, new ArrayList());
		return lista;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public List<Tabla> findDetalleTablaByValuePadre(String value,
			String codigoPadre) throws BOException, DAOException {
		String where = "select o from "+Tabla.class.getName()+" o where 1=1 ";
		where+=" and o.padre.codigo = '"+codigoPadre+"'";
		where+=" and o.valor = '"+value+"'";
		
		List<Tabla> lista = super.executeHqlParam(where, new ArrayList());
		return lista;
	}
	
	@SuppressWarnings("unchecked")
	public List<Tabla> findTablaCodigo(String prefijo) throws BOException, DAOException{
		String where = " where codigo like '"+ prefijo +"%' and padre <> null";
		List<Tabla> lista = super.executeQuery(Tabla.class, where, null);
		return lista;
	}

	@SuppressWarnings("unchecked")
	public List<Tabla> findTipoCorreo()throws BOException, DAOException{
		String where = " where abreviado like 'TC%' and padre <> null";
		List<Tabla> lista = super.executeQuery(Tabla.class, where, null);
		return lista;
	}
	
	@SuppressWarnings("unchecked")
	public List<Tabla> findHijos(Long idPadre)throws BOException, DAOException{
		String where = " where padre = '"+idPadre+"'";
		List<Tabla> lista = super.executeQuery(Tabla.class, where, null);
		return lista;
	}
}